package nl.hanze.designpatterns.Views.QnA;

import nl.hanze.designpatterns.Controllers.QnAController;
import nl.hanze.designpatterns.Controllers.TicketController;
import nl.hanze.designpatterns.Domain.Answer;
import nl.hanze.designpatterns.Domain.Question;
import nl.hanze.designpatterns.Domain.TroubleTicket;
import nl.hanze.designpatterns.Models.MainModel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Aron on 6-10-2014.
 */
public class QnAScreen extends JPanel implements ActionListener, ListSelectionListener, Observer {
    private JPanel mainPanel;
    private JPanel panel1;
    private JLabel AsLabel;
    private JLabel UserLabel;
    private JButton logoutButton;
    private JButton ticketButton;
    private JList questionsList;
    private JButton selectButton;
    private JLabel CurrentQuestionPanel;
    private JButton LastQuestionButton;
    private JTextPane awnserPane;
    private JList awnserList;


    private QnAController controller;
    private MainModel mainModel;

    public QnAScreen(QnAController controller, MainModel mainModel) {
        this.controller = controller;
        this.mainModel = mainModel;

        awnserPane.setEditable(false);

        add(panel1);
        mainModel.addObserver(this);
        questionsList.setModel(mainModel.getQuestionList());
        awnserList.setModel(mainModel.getAnswerList());
        addListeners();
        updateValues();
    }

    private void updateValues() {
        System.out.println("<QnAScreen>[setValues]");
        String loggedInUser = mainModel.getLoggedInUser();

        UserLabel.setText(loggedInUser);

        updateQuestionValues();
    }

    private void updateQuestionValues(){
        CurrentQuestionPanel.setText(mainModel.selectedQuestion.getText());
        System.out.print("    "+mainModel.selectedQuestion.getText());
        updateAnswerValues();
    }

    private void updateAnswerValues(){
        awnserPane.setText(mainModel.selectedAnswer.getText());
    }

    private void addListeners(){
        //logout button
        logoutButton.setActionCommand("logout");
        logoutButton.addActionListener(this);

        //ticketButton
        ticketButton.setActionCommand("toTickets");
        ticketButton.addActionListener(this);

        //LastQuestionButton
        LastQuestionButton.setActionCommand("lastQuestion");
        LastQuestionButton.addActionListener(this);

        //selectButton
        selectButton.setActionCommand("selectQuestion");
        selectButton.addActionListener(this);

        //awnserList
        awnserList.addListSelectionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand())
        {
            case "logout":
                System.out.println("[ACTION]logout");
                this.controller.logout();
                break;
            case "toTickets":
                System.out.println("[ACTION]toTickets");
                this.controller.toTickets();
                break;
            case "lastQuestion":
                System.out.println("[ACTION]lastQuestion");
                this.controller.selectQuestion(mainModel.selectedQuestion.parent);
                break;
            case "selectQuestion":
                System.out.println("[ACTION]selectQuestion");
                this.controller.selectQuestion((Question) questionsList.getSelectedValue());
                //TODO open selected
                break;
            default:
                System.out.println("[ACTION]unknown: "+e.getActionCommand());
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {

        System.out.println("[SELECTED]awnserList: "+ awnserList.getSelectedValue());
        if (e.getSource().equals(awnserList)) {
            controller.selectAnswer((Answer)awnserList.getSelectedValue());
        }

    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof String) {
            if (arg.equals("question")) {
                updateQuestionValues();
            } else if (arg.equals("answer")) {
                updateAnswerValues();
            }
        }

    }
}
